package main

import (
	"database/sql"
	_"github.com/go-sql-driver/mysql"
	"time"
)

const LOG_STATUS_SUCC = 1
const LOG_STATUS_FAIL = 2

//获取可调度任务
func getTaskList() (map[string]map[string]string) {
	query_sql := "select t1.id as id,t1.queue_name as queue,t1.api_url,t2.channel_name as channel,t2.exchange_name as exchange,t2.route_key,t2.type from message_consumer t1 left join message_producter t2 on t1.pid = t2.id where t1.status = 2"
	taskData := queryMultiple(query_sql)
	ret := make(map[string]map[string]string)
	for _, task := range taskData {
		ret[task["id"]] = task
	}
	return ret
}

//根据id获取可执行Url
func getTaskInfoById(id string) (string) {
	query_sql := "select api_url from message_consumer where id ="+ id
	ret := querySingle(query_sql)
	return ret["api_url"]
}


func queryMultiple(query_sql string, args... interface{}) (map[int]map[string]string) {
	//查询数据库
	db := connect()
	rows, err := db.Query(query_sql, args...)
	defer db.Close()
	if (err != nil) {
		panic(err.Error())
	}

	//构造scanArgs、values两个数组，scanArgs的每个值指向values相应值的地址
	columns, _ := rows.Columns()
	scanArgs := make([]interface{}, len(columns))
	values := make([][]byte, len(columns))
	for i := range values {
		scanArgs[i] = &values[i]
	}

	//最后得到的map
	i := 0
	ret := make(map[int]map[string]string)
	for rows.Next() {
		err = rows.Scan(scanArgs...);
		record := make(map[string]string) //每行数据
		for k, v := range values { //每行数据是放在values里面，现在把它挪到row里
			record[columns[k]] = string(v)
		}
		ret[i] = record //装入结果集中
		i++
	}

	return ret
}

func querySingle(query_sql string, args... interface{}) (map[string]string) {
	//查询数据库
	db := connect()
	rows, err := db.Query(query_sql, args...)
	defer db.Close()
	if (err != nil) {
		panic(err.Error())
	}

	//构造scanArgs、values两个数组，scanArgs的每个值指向values相应值的地址
	columns, _ := rows.Columns()
	scanArgs := make([]interface{}, len(columns))
	values := make([][]byte, len(columns))
	for i := range values {
		scanArgs[i] = &values[i]
	}
	rows.Next()
	err = rows.Scan(scanArgs...);
	record := make(map[string]string) //每行数据
	for k, v := range values { //每行数据是放在values里面，现在把它挪到row里
		record[columns[k]] = string(v)
	}

	return record
}

func insertTaskLog(message_id int64,api_url string,output string) int64 {
	//写入任务执行日志
	insert_sql := "insert into message_api_access_log (message_id, api_url,output,add_time) values (?, ?, ?, ?)"
	nowtime := time.Now().Format("2006-01-02 15:04:05")
	run_id := insert(insert_sql, message_id, api_url,output,nowtime)

	return run_id
}

func insert(insert_sql string, args... interface{}) int64{
	db := connect()
	res, err := db.Exec(insert_sql, args...)
	defer db.Close()
	if (err != nil) {
		panic(err.Error())
	}
	id, _ := res.LastInsertId()
	return id
}

func update(update_sql string, args... interface{}) bool{
	db := connect()
	_, err := db.Exec(update_sql, args...)
	defer db.Close()
	if (err != nil) {
		panic(err.Error())
	}
	return true
}


func connect() (*sql.DB) {
	config := GetConfig()
	dsn := config.Mysql.Username + ":" + config.Mysql.Password + "@tcp(" + config.Mysql.Host + ")/" + config.Mysql.Dbname + "?charset=" + config.Mysql.Charset
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		panic(err.Error())
	}
	return db
}


